//
//  M4NetworkTarget.swift
//  M4
//
//  Created by YuChuang on 2024/8/7.
//

import Foundation
import Moya

class M4NetworkTarget: TargetType {
    
    var M4_path: M4NetworkAPI = .m4version
    var M4_method: Moya.Method = .post
    var M4_params: [String:Any] = [:]
    var M4_headers: [String:String] = [:]
    var M4_formData: [MultipartFormData] = []

    class func M4_targetType(_ url: M4NetworkAPI = .m4version,
                          _ method: Moya.Method = .post,
                           params: [String:Any] = [:],
                           headers: [String:String] = [:],
                           formData: [MultipartFormData] = []
    ) -> M4NetworkTarget {
        let target = M4NetworkTarget.init()
        target.M4_path = url;
        target.M4_method = method
        target.M4_params = params
        target.M4_headers = headers
        target.M4_formData = formData
        return target
    }

    var baseURL: URL {
        if M4_path == .m4uploadFile {
            return URL.init(string: M4NetworkAPI.filePath.rawValue)!
        }
        return URL.init(string: M4NetworkAPI.basePath.rawValue)!
    }

    var path: String {
        return M4_path.rawValue
    }

    var method: Moya.Method { return M4_method }

    var task: Task {
        return .requestParameters(parameters: M4_params, encoding: method == .post ? JSONEncoding.default : URLEncoding.default)
    }

    var headers: [String: String]? {
        
        var headerParam:[String:String] = M4_headers
        headerParam = [
            "isSecert": "0",
            "code": M4UserDataManager.m4UserShare.M4Itemcode,
            "content-type": "application/json"
        ]
        
        return headerParam
    }
    
}
